package com.ruoyi.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.domain.entity.Device;
import com.ruoyi.common.core.domain.entity.Warning;
import com.ruoyi.common.core.domain.entity.WarningMess;
import com.ruoyi.common.enums.SmsTemplateCode;
import com.ruoyi.common.utils.SmsUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.DeviceMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.WarningMapper;
import com.ruoyi.system.service.WarningService;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author: ruohao
 * @Date: 2023/9/14
 **/
@Component
@Service
public class WarningServiceImpl implements WarningService {
    @Resource
    private WarningMapper warningMapper;

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private DeviceMapper deviceMapper;

    @Override
    public List<Warning> selectList(Warning warning) {
        return warningMapper.selectList(warning);
    }

    @Override
    public Warning selectById(Long id) {
        return warningMapper.selectById(id);
    }

    @Override
    public Boolean create(Warning warning) {
        return warningMapper.insert(warning) == 1;
    }

    @Override
    public Boolean update(Warning warning) {
        return warningMapper.update(warning) == 1;
    }

    @Override
    public Boolean deleteById(Long id) {
        return warningMapper.deleteById(id) == 1;
    }

    @Override
    public List<Warning> selectFiveList() {
        return warningMapper.selectFiveList();
    }

    @Override
    public Integer selectListCount(String startTime, String endTime, String type) {
        return warningMapper.selectListCount(startTime, endTime, type);
    }

    @Override
    public List<Warning> selectTenList(Warning warning) {
        List<Warning> list = warningMapper.selectTenList(warning);
        return list;
    }

    public void autoWarning() {
        List<Warning> warnings = warningMapper.selectListWarning();
        //发送短信
        //查询手机号
        for (Warning warning : warnings) {
            List<String> phones = sysUserMapper.getSmsPhonesWarning();
            for (String phone : phones) {
                WarningMess param = new WarningMess();
                param.setDeviceID(warning.getDeviceId());
                param.setDeviceType(warning.getType());
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                if (warning.getWarningTime() == null) {
                    param.setTime(sdf.format(new Date()));
                } else {
                    param.setTime(sdf.format(warning.getWarningTime()));
                }
                param.setContent(warning.getContent());

                param.setAddress("");

                Device query = new Device();
                query.setDeviceId(param.getDeviceID());
                List<Device> device = deviceMapper.selectList(query);
                if (!CollectionUtils.isEmpty(device)) {
                    if (StringUtils.isNotBlank(device.get(0).getLocation())) {
                        param.setAddress(device.get(0).getLocation());
                    }
                }

//                JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(param));
                SmsUtils.sendSms(SmsTemplateCode.WARNING.getCode(), phone, JSON.toJSONString(param));
            }

            warning.setRelease("1");
            warningMapper.update(warning);
        }
    }
}
